package com.wgc.vip.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wgc.vip.domain.Vip;
import com.wgc.vip.service.VipService;
import com.wgc.vip.mapper.VipMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 15776
* @description 针对表【vip】的数据库操作Service实现
* @createDate 2024-07-28 12:30:29
*/
@Service
public class VipServiceImpl extends ServiceImpl<VipMapper, Vip>
    implements VipService{
@Autowired
private VipMapper vipMapper;
    @Override
    public List<Vip> selectAll() {
        LambdaQueryWrapper<Vip> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Vip::getIsDelete, 0);
        List<Vip> vipList = vipMapper.selectList(lqw);
        return vipMapper.selectList(lqw);
    }

    @Override
    public Integer insert(Vip vip) {
        LambdaQueryWrapper<Vip> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Vip::getVipName, vip.getVipName());
        List<Vip> vipList = vipMapper.selectList(lqw);
        if (!vipList.isEmpty()) {
            return -1;
        }
        String gensalt = BCrypt.gensalt();
        String saltPassword = BCrypt.hashpw(vip.getPassword(), gensalt);
        vip.setPassword(saltPassword);
        return  vipMapper.insert(vip);
    }

    @Override
    public Integer update(Vip vip) {
        return vipMapper.updateById(vip);
    }

    @Override
    public Integer delete(Integer id) {
        LambdaQueryWrapper<Vip> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Vip::getId, id);
        Vip vip = vipMapper.selectOne(lqw);
        if (vip != null) {
            vip.setIsDelete(1);
            return vipMapper.updateById(vip);
        }
        return -1;
    }
    
    @Override
    public List<Vip> selectList(Vip vip) {
        return vipMapper.selectList(lqw(vip));
    }

    @Override
    public Page<Vip> pageAll(Integer page, Integer size) {
        Page<Vip> page1 = new Page<>(page, size);
        return this.page(page1, null);
    }

    @Override
    public Page<Vip> pageList(Vip vip, Integer page, Integer size) {
        Page<Vip> page1 = new Page<>(page, size);
        return this.page(page1, lqw(vip));
    }

    @Override
    public int status(Long id, Integer status) {
        LambdaQueryWrapper<Vip> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Vip::getId, id);
        Vip vip = vipMapper.selectOne(lqw);
        vip.setStatus(status);
        return vipMapper.updateById(vip);
    }

    @Override
    public Integer login(String vipName, String password) {
        LambdaQueryWrapper<Vip> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Vip::getVipName, vipName);
        Vip vip = vipMapper.selectOne(lqw);
        if (vip == null) {
            return -2;
        }
        if(vip.getStatus() == 1){
            return -3;
        }
        if (!BCrypt.checkpw(password, vip.getPassword())) {
            return 1;
        }else {
            return -1;
        }
    }

    public LambdaQueryWrapper<Vip> lqw(Vip iVip) {
        LambdaQueryWrapper<Vip> lqw = new LambdaQueryWrapper<>();
        if (iVip.getId() != null) {
            lqw.eq(Vip::getId, iVip.getId());
        }
        if (iVip.getVipName() != null) {
            lqw.eq(Vip::getVipName, iVip.getVipName());
        }
        if (iVip.getNickName() != null) {
            lqw.eq(Vip::getNickName, iVip.getNickName());
        }
        if (iVip.getTel() != null) {
            lqw.eq(Vip::getTel, iVip.getTel());
        }
        return lqw;
    }

}




